<script>
	import config from "@/config/config.js";
	import VueEvent from "./VueEvent";
	import cal from "./cal/vbt.js";
	export default {
		data() {
			return {
				fontAdded: false, // 新增一个标志，用于判断字体是否已经添加
				
				};
		},
		onLaunch: function() {
			this.initConfig();
			this.check_login();
			VueEvent.init_Clock_Audio()
		},
		onShow() {
			wx.loadFontFace({
				global: true,
				family: "优设标题黑",
				source: "https://tinghetech.com/other/YouSheBiaoTiHei.ttf",
				success: function() {
					console.log("字体加载成功");
				},
				fail: function(err) {
					console.error("字体加载失败", err);
				},
				complete: function() {
					// 无论成功或失败都会执行
				},
			});

		},
		onLoad() {
			this.$tn.vuex("vuex_frenidList", [{
					name: "林允儿",
					age: 22,
				},
				{
					name: "刘亦菲",
					age: 21,
				},
				{
					name: "周淑怡",
					age: 23,
				},
			]);
		},
		methods: {

			playAudio(src) {
				const audioContext = uni.createInnerAudioContext(); // 创建音频上下文对象
				audioContext.src = src; // 设置音频源，替换为你的音频文件路径
				audioContext.autoplay = true; // 设置自动播放

				// 监听播放事件
				audioContext.onPlay(() => {
					console.log('音频开始播放');
				});

				// 监听播放错误事件
				audioContext.onError((res) => {
					console.log('音频播放出错：', res.errMsg);
				});

				// 监听播放完成事件
				audioContext.onEnded(() => {
					console.log('音频播放完毕');
				});

				// 播放音频
				audioContext.play();
			},

			// 挂载全局配置信息
			initConfig() {
				this.globalData.config = config;
				// VueEvent.sharedData.pinlv = cal.simple_new_test(1,1,100,80,0,0);
				// cal.simple_new([],[],[],[],[],[],1,1,100,80,0,0);
			},
			check_login() {
				// 获取本地存储的登录状态
				const isLoggedIn = uni.getStorageSync("hashLogin");

				if (!isLoggedIn) {
					// 未登录，跳转到登录页
					// console.log("未登录，跳转登录页");
					// uni.navigateTo({
					// 	url: "/pages/loginA/loginA", // 登录页面路径
					// });
				} else {}
			},
			addFontFace() {
				console.log("执行le");
				const style = document.createElement("style");
				style.textContent = `
					@font-face {
					  font-family: '优设标题黑';
					  src: url('${this.fontUrl}') format('truetype');
					}
				  `;
				document.head.appendChild(style);
			},
			textToVoice(text) {
				const systemInfo = wx.getSystemInfoSync();
				console.log("当前设备系统：", systemInfo.platform);

				const pluginSI = requirePlugin("WechatSI");
				const innerAudioSI = wx.createInnerAudioContext();

				pluginSI.textToSpeech({
					lang: "zh_CN",
					tts: true,
					content: text,
					success: async (res) => {
						console.log("TTS 返回的音频 URL:", res.filename);

						// 先下载音频文件
						const downloadTask = wx.downloadFile({
							url: res.filename,
							success: (downloadRes) => {
								console.log("下载成功，临时路径:", downloadRes.tempFilePath);
								innerAudioSI.src = downloadRes.tempFilePath; // 使用本地临时文件
								innerAudioSI.play();

								innerAudioSI.onEnded(() => {
									console.log("语音播放完毕");
									innerAudioSI.stop();
									innerAudioSI.destroy();
								});
							},
							fail: (err) => {
								console.error("下载音频失败:", err);
							}
						});

						downloadTask.onProgressUpdate((res) => {
							console.log(`下载进度: ${res.progress}%`);
						});
					},
					fail: (res) => {
						console.log("TTS 失败:", res);
						if (systemInfo.platform === "ios") {
							console.log("iOS 设备上 TTS 失败");
						}
					}
				});

				return innerAudioSI;
			},
		}
	}
</script>
<style lang="scss">
	// 引入Tuniao-Ui的样式文件(如果还需引入uView或者其他框架，如下即可)
	@import "./tuniao-ui/index.scss";
	@import "./tuniao-ui/iconfont.css";
	// 在使用animate.css的时候注意一定要把animate.css文件中的 :root选择器改为 page (切记)。
	@import "@/static/animate/animate.css";

	@font-face {
		// font-family: '优设标题黑';
		// src: url('https://636c-cloudbase-7gllhoqe13a5c156-1331321732.tcb.qcloud.la/%E4%BC%98%E8%AE%BE%E6%A0%87%E9%A2%98%E9%BB%91.ttf?sign=43bdcd11cd2aaea4618a394824858305&t=1743406852') format('truetype');
	}

	page {

		// background-color: rgba(110, 88, 170, 0.02) !important;
	}
</style>